//
//  ForgetPswViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/17.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "FPPswCheckView.h"
#import "FPPhoneCodeView.h"
#import "LoginHandleModel.h"
#import "ForgetPswViewController.h"
#import "UIButton+CountDown.h"

@interface ForgetPswViewController ()

@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@property (weak, nonatomic) IBOutlet UIView *inputBGView;

@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (nonatomic, strong) FPPhoneCodeView *phoneCodeView;
@property (nonatomic, strong) FPPswCheckView *pswEditView;

@property (nonatomic, strong) NSMutableArray *stepArray;

@end

@implementation ForgetPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
    });
}

- (void)initView {
    
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    self.view.backgroundColor = UIColorHex(0xffffff);
    
    [self configPhoneCodeView];
    [self configCheckPswView];
 
}


- (IBAction)goBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)smsAction {
    
    if ([Utilities checkPhoneTel:self.phoneCodeView.phoneTF.text]) {
        [self request_changePswSms];
    } else {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        [SVProgressHUD dismissWithDelay:1];
    }
}

- (IBAction)handleAction:(id)sender {
    
    if (![Utilities checkPhoneTel:self.phoneCodeView.phoneTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    if (self.phoneCodeView.codeTF.text.length != 6) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的验证码"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    if (![self.pswEditView.pswTF.text isEqualToString:self.pswEditView.checkPswTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"两次密码输入不一致，请检查"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    [self requrst_changePsw];
}

- (void)configPhoneCodeView {
    
    [self.inputBGView addSubview:self.phoneCodeView];
    [self.phoneCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(104);
    }];
    if (![self.stepArray containsObject:@(TT_ForgetPswStepType_phoneCode)]) {
        [self.stepArray addObject:@(TT_ForgetPswStepType_phoneCode)];
    }
    self.titleLab.text = @"重置密码";
}

- (void)configCheckPswView {
    
    [self.inputBGView addSubview:self.pswEditView];
    [self.pswEditView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(104);
    }];
    if (![self.stepArray containsObject:@(TT_ForgetPswStepType_phoneCode)]) {
        [self.stepArray addObject:@(TT_ForgetPswStepType_phoneCode)];
    }
    [self.handleBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.handleBtn setBackgroundImage:[UIImage imageNamed:@"rect_orange"] forState:UIControlStateNormal];

}

- (FPPhoneCodeView *)phoneCodeView {
    if (!_phoneCodeView) {
        _phoneCodeView = [[NSBundle mainBundle] loadNibNamed:@"FPPhoneCodeView"owner:nil options:nil].firstObject;
        [_phoneCodeView.countDownBtn addTarget:self action:@selector(smsAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _phoneCodeView;
}

- (FPPswCheckView *)pswEditView {
    if (!_pswEditView) {
        _pswEditView = [[NSBundle mainBundle] loadNibNamed:@"FPPswCheckView"owner:nil options:nil].firstObject;
    }
    return _pswEditView;
}

- (void)requrst_changePsw {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.phoneCodeView.phoneTF.text forKey:@"userName"];
    [dic setValue:self.phoneCodeView.codeTF.text forKey:@"vcode"];

    [dic setValue:@"4" forKey:@"loginType"];

    [dic setValue:self.pswEditView.pswTF.text forKey:@"passWord"];
    [dic setValue:self.pswEditView.checkPswTF.text forKey:@"confirmPassWord"];


    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ChangePsw];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];

        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                 
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
                
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

- (void)request_changePswSms {

    [self.phoneCodeView.codeTF becomeFirstResponder];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.phoneCodeView.phoneTF.text forKey:@"mobile"];
    [dic setValue:@"3" forKey:@"type"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SMS];

    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [SVProgressHUD dismiss];

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                [weakSelf.phoneCodeView.countDownBtn startWithTime:59
                                           title:@"重新获取"
                                  countDownTitle:@"s"
                                       mainColor:UIColor.clearColor
                                      countColor:UIColor.clearColor];
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:@"请求异常"];
        [SVProgressHUD dismissWithDelay:1];
    }];
    
}

@end
